from ronglian_sms_sdk import SmsSDK

_accId = '8a216da87ba59937017bb05691c60158'
_accToken = 'b576690182084d2c94f7a850fecefd27'
_appId = '8a216da87ba59937017bb05692d5015e'

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

# def send_message():
#     sdk = SmsSDK(accId, accToken, appId)
#     tid = 1
#     mobile = '18665895846'
#     datas = ('12345', 5)
#     resp = sdk.sendMessage(tid, mobile, datas)
#     print(resp)


# def send_message():
#     sdk = SmsSDK(_accId, _accToken, _appId)
#     tid = '容联云通讯平台创建的模板'
#     mobile = '手机号1,手机号2'
#     datas = ('变量1', '变量2')
#     resp = sdk.sendMessage(tid, mobile, datas)
#     print(resp)

class CCP(object):
    """发送短信验证码的单例类"""
    def __new__(cls, *args, **kwargs):
        """
        定义单例的初始化方法
        ：:return ：单例
        """
        # 判断单例是否存在:_instanse属性中存储的就是单例   hasahttr
        # cls代表CCP
        if not hasattr(cls, '_instance'):
            # 如果不单例存在，初始化单例
            cls._instance = super(CCP, cls).__new__(cls,*args, **kwargs)

            # 初始化 SDK
            cls._instance.smssdk = SmsSDK(_accId, _accToken, _appId)
            # rest = REST(_serverIP, _serverPort, _softVersion)
            # rest.setAccount(_accountSid, _accountToken)
            # rest.setAppId(_appId)
            # cls._instance.rest = REST(_serverIP, _serverPort, _softVersion)
            # cls._instance.rest.setAccount(_accountSid, _accountToken)
            # cls._instance.rest.setAppId(_appId)

        # 返回单例
        return cls._instance

    def send_template_sms(self,tid, mobile, datas):
        """发送短信验证码的单例方法
        :param to:手机号
        :param datas:内容数据
        :param tempId:模版ID
        :return 成功：0 失败 -1
        """
        result = self.smssdk.sendMessage(tid, mobile, datas)
        print(2)
        print(result)

        # if result.get('statusCode') == '000000':
        #     return 0
        # else:
        #     return -1
# if __name__ == '__main__':
    # 注意： 测试的短信模板编号为1
    # sendTemplateSMS('18665895846', ['123456', 5], 1)
    # 单例类发送短信验证码
    # print(1)
    # CCP().send_template_sms(1, '18665895846', ['123456', 5])
